import axios from "axios";
import Compressor from "compressorjs";
import service from '../uils/request'
// 此处演示，手写了token，后期从服务器上获取
const token =
  "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJ0ZXN0Iiwicm9sZSI6IjMiLCJwcm9qZWN0SWQiOlsiMSIsIjIiXSwiYWRkVGltZSI6IjIwMjMtMDktMDMgMDE6NDI6MjUiLCJkZXNjcmlwdGlvbiI6Iumaj-aJi-aLjea1i-ivlSIsImlhdCI6MTY5Mzg4NDQ3M30.bOMK5-lra04nT5ReRwPezBOi4wXLxVacuV8hUfeYVaY";

//获取随手拍事件
export function getarticle() {
  return service({
    url: `/article`,
    method: 'get',
  })
}
//获取党员信息
export function getpartymember() {
  return service({
    url: `/partymember?current=1&pageSize=1000`,
    method: 'get',
  })
}
//获取党组织信息
export function getpartyorginaze() {
  return service({
    url: `/partygroup?current=1&pageSize=1000`,
    method: 'get',
  })
}
//获取活动信息
export function getpartyaction() {
  return service({
    url: `/partyaction?current=1&pageSize=1000`,
    method: 'get',
  })
}





//获取/auth/profile
export function getprofile() {
  return service({
    url: `/auth/profile`,
    method: 'get',
  })
}
//获取企业信息
export function getenterprise() {
  return service({
    url: `/enterprise`,
    method: 'get',
  })
}
//
export function getsummary() {
  return service({
    url: `/enterprise/summary`,
    method: 'get',
  })
}
//获取党员信息
export function getorganize() {
  return service({
    url: `/party-member?current=1&pageSize=1000`,
    method: 'get',
  })
}
//获取党员信息/api/party-organization
export function getpartyorganization() {
  return service({
    url: `/party-organization?current=1&pageSize=1000`,
    method: 'get',
  })
}
//获取党建活动信息
export function getorganizeparty() {

  return service({
    url: `/party-activity`,
    method: 'get',
  })
}
//获取概览信息
export function getoverview(name) {
  return service({
    url: `/punan-overview`,
    method: 'get',
    data: {
      name
    }
  })
}
//获取收入
export function getrevenue() {
  return service({
    url: `/revenue`,
    method: 'get',
  })
}
//获取人房信息
export function getrenfang() {
  return service({
    url: `/ren-fang`,
    method: 'get',
  })
}
//获取农产品信息/api/crop-detail/nongchanpin
export function getcropdetail() {
  return service({
    url: `/crop-detail/nongchanpin`,
    method: 'get',
  })
}
//获取企业图片
export function getcphoto(type) {
  return service({
    url: `/photo-info?current=1&pageSize=1000`,
    method: 'get',
    params: {
      type
    }
  })
}
//api/map - info
export function getspotphoto() {
  return service({
    url: `/map-info?current=1&pageSize=1000`,
    method: 'get',
  })
}
//获取关爱活动列表&activityType=love
export function getLove() {
  return service({
    url: `/party-activity?current=1&pageSize=1000&activityType=love`,
    method: 'get',
  })
}
/**
 * 上传图片
 * @param {*} file
 * @returns
 */
export const uploadFile = async (file) => {
  console.log("upload", file);

  const url = "api/photo";

  // 压缩图片
  const compressor = new Promise((resolve, reject) => {
    new Compressor(file, {
      quality: 0.4,
      convertSize: 1,

      // The compression process is asynchronous,
      // which means you have to access the `result` in the `success` hook function.
      success(result) {
        console.log(result);
        const formData = new FormData();

        // The third parameter is required for server
        formData.append("file", result, result.name);

        console.log(
          "压缩后大小为：",
          (result.size / 1024 / 1024).toFixed(1),
          "M"
        );

        // Send the compressed image file to server with XMLHttpRequest.
        resolve(
          axios.post(url, formData, {
            headers: {
              Authorization: `Bearer ${token}`,
              "Content-Type": "multipart/form-data",
            },
          })
        );
      },
      error(err) {
        console.log(err.message);
        reject("失败");
      },
    });
  });

  const res = await compressor;

  return res;
};

/**
 * 获取图库中图片的blob对象
 * @param {string} fileName 图片名称
 * @returns
 */
export const getImageBuffer = async (fileName) => {
  const res = await axios.get("api/photo", {
    params: { fileName: fileName },
    headers: {
      Authorization: `Bearer ${token}`,
    },
  });

  const imgBlob = new Blob([
    new Uint8Array(res.data.data.data, 0, res.data.data.data.length),
  ]);
  const url = URL.createObjectURL(imgBlob);
  return url;
};

/**
 * 用户登陆
 * @returns
 */
export const login = () => {
  const res = axios.post("api/auth/userLogin", {
    username: "test",
    password: "test",
  });

  return res;
};

